package GameObjects;
import java.util.Date;

import utils.myVector;

import com.jme.scene.Node;
import com.jme.scene.Spatial;
import utils.myVector;
 

public class Bullet extends Node {
    private Spatial model;

    private float myRadius, myMass, currentHealth, maxHealth, mySpeed;
    private myVector location = new myVector(0,0);
	private myVector movementDirection = new myVector(0, 0);
	private myVector velocity = new myVector(0, 0);
	private myVector acceleration = new myVector(0, 0);
	public Date prevTime;
	private String name;
	
	boolean movingNorth, movingSouth, movingWest, movingEast;


    public Bullet(String id, Spatial model, 
    		String n, myVector _location, float _mass, float _radius, float _speed, float _health){
    	
        super(id);
        setModel(model);
        
        location.setVector(_location);
		name = n;
		myRadius = _radius;
		myMass = _mass;
		currentHealth = _health;
		maxHealth = _health;
		mySpeed = _speed;
		prevTime = new Date();
		
		resetMovementDirection();
    }
    
    public void updatePosition(){
    	long timestep = new Date().getTime() - prevTime.getTime();
		prevTime = new Date();
				
		float timestepSeconds = timestep/1000f;
		
		location = location.add(
				velocity.multiplyByConst(timestepSeconds).add(
						acceleration.multiplyByConst(.05f * timestepSeconds * timestepSeconds)));
				
		velocity = velocity.add(
				acceleration.add(movementDirection.multiplyByConst(mySpeed)).
				multiplyByConst(timestepSeconds));
		
		this.setLocalTranslation(location.x, 0, location.y);
    	
    }
    
    public void setMoveNorth(){
    	movingNorth = true;
    	movingSouth = false;
    	setMovementDirection();
    }
    public void setMoveSouth(){
    	movingSouth = true;
    	movingNorth = false;
    	setMovementDirection();
    }
    public void setMoveWest(){
    	movingWest = true;
    	movingEast = false;
    	setMovementDirection();
    }
    public void setMoveEast(){
    	movingEast = true;
    	movingWest = false;
    	setMovementDirection();
    }
    
    public void resetMovementDirection(){
    	movingNorth = false;
    	movingSouth = false;
    	movingWest = false;
    	movingEast = false;
    	
    	setMovementDirection();
    }
    
    private void setMovementDirection(){
    	int a=0, b=0;
    	
    	if(movingNorth) b=1;
    	if(movingSouth) b=-1;
    	if(movingWest) a=1;
    	if(movingEast) a=-1;
    	
    	movementDirection.setVector(a, b);
    	movementDirection.normalize();
    }
 
    public Spatial getModel() {
        return model;
    }
 
    /**
     * sets the model spatial of this vehicle. It first
     * detaches any previously attached models.
     * @param model the model to attach to this vehicle.
     */
    public void setModel(Spatial model) {
        this.detachChild(this.model);
        this.model = model;
        this.attachChild(this.model);
    }
    
    public myVector getLocation(){
    	return location;
    }
}